import requests
import json

# get请求，通过浏览器地址栏发起的请求，get请求的请求参数再url后面
# post请求，请求的参数再body, 一班用于登录上传用户名密码的时候


# 请求的路径
url = "https://m.weibo.cn/comments/hotflow?id=4750848993266460&mid=4750848993266460&max_id_type=0"
# 发起请求, 返回请求的结果
response = requests.get(url=url)

# 获取请求返回的结果数据
text = response.text
print(text)

# 将返回的数据转换成json
json_loads = json.loads(text)

with open("com.txt", mode="w", encoding="utf-8") as f:
    for data in json_loads["data"]["data"]:
        # 评价的事件
        created_at = data["created_at"]
        # 评价的内容
        text = data["text"]
        # 点赞数
        like_count = data["like_count"]

        # 取出用户
        user = data["user"]
        user_id = user["id"]
        screen_name = user["screen_name"]
        gender = user["gender"]

        line = "%s\t%s\t%s\t%s\t%s\t%s" % (user_id, screen_name, gender, like_count, created_at, text)

        # 保存数据
        f.write(line)
        f.write("\n")

        print(line)
